package com.scorpio.order.service.impl;


import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.scorpio.redis.cachelock.RedisLock;
import com.scorpio.redis.cachelock.RedisLockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scorpio.order.mapper.OrderWaybillMapper;
import com.scorpio.order.domain.OrderWaybillEntity;
import com.scorpio.order.service.IOrderWaybillService;
import com.scorpio.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单物流信息Service业务层处理
 *
 * @author scorpio
 * @date 2022-01-11
 */
@Service
public class OrderWaybillServiceImpl extends BaseServiceImpl<OrderWaybillMapper, OrderWaybillEntity> implements IOrderWaybillService {

    @Autowired
    private RedisLock redisLock;

    //新增
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(OrderWaybillEntity entity) {
        String lockKey = "OrderWaybillServiceImpl_save:" + entity.getOrderOn();
        if (redisLock.lock(lockKey, 5)) {
            try {
                OrderWaybillEntity waybillEntity = baseDao.selectOrderOn(entity.getOrderOn());
                if (waybillEntity == null) {
                    entity.setUpdateTime(new Date());
                    return baseDao.insert(entity);
                } else {
                    waybillEntity.setContent(entity.getContent());
                    return baseDao.updateById(waybillEntity);
                }
            } finally {
                redisLock.unlock(lockKey);
            }
        }
        throw new RedisLockException();
    }

}
